package sis.uitest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-08-04 16:31
 * @notify
 * @version 1.0
 */

import com.sun.org.apache.regexp.internal.RE;
import junit.framework.TestCase;
import oracle.jrockit.jfr.JFR;
import sis.myjunit.Ignore;
import sis.studentinfo.Course;
import sis.ui.CoursesPanel;
import sis.ui.Sis;
import sis.ui.UpcaseFilter;
import sis.ui.Util;
import sis.util.ImageUtil;

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;
import java.awt.*;
import java.awt.event.InputEvent;
import java.util.concurrent.locks.Condition;

public class SisTest extends TestCase {
    //Sis对象
    private Sis sis;
    //窗体对象
    private JFrame frame;

    //面板对象
    private CoursesPanel panel;

    //模拟工具
    private Robot robot;

    //创建 Sis 获取窗体
    protected void setUp() throws Exception {
        sis = new Sis();
        frame = sis.getFrame();
        panel = (CoursesPanel) Util.getComponent(frame, CoursesPanel.NAME);
        robot = new Robot();
    }

    public void testAddCourse() {
        //从窗体中拿到面板
        CoursesPanel panel = (CoursesPanel) Util.getComponent(frame, CoursesPanel.NAME);
        //给面板中的输入框设置值
        panel.setText(CoursesPanel.DEPARTMENT_FIELD_NAME, "MATH");
        panel.setText(CoursesPanel.NUMBER_FIELD_NAME, "300");

        //获取按钮
        JButton button = panel.getButton(CoursesPanel.ADD_BUTTON_NAME);
        panel.setEnabled(CoursesPanel.ADD_BUTTON_NAME, true);
        //点击
        button.doClick();

        Course course = panel.getCourse(0);
        assertEquals("MATH", course.getDepartment());
        assertEquals("300", course.getNumber());
    }

    public void testCreate() {
        //测试窗体1的尺寸
        final double tolerance = 0.05;
        assertEquals(Sis.HEIGHT, frame.getSize().getHeight(), tolerance);
        assertEquals(Sis.WIDTH, frame.getSize().getWidth(), tolerance);
        assertEquals(JFrame.EXIT_ON_CLOSE, frame.getDefaultCloseOperation());

        //查询窗体下有没有 指定名称的面板
        assertNotNull(Util.getComponent(frame, CoursesPanel.NAME));
        assertEquals(Sis.COURSES_TITLE, frame.getTitle());

        Image image = frame.getIconImage();
        assertEquals(image, ImageUtil.create("/fans_icon.png"));

        //测试过滤器
        CoursesPanel panel =(CoursesPanel) Util.getComponent(frame,CoursesPanel.NAME);
        assertNotNull(panel);
        verifyFilter(panel);
    }

    //测试过滤器是否存在
    private void verifyFilter(CoursesPanel panel){
        DocumentFilter filter = getFilter(panel,CoursesPanel.DEPARTMENT_FIELD_NAME);
        assertTrue(filter.getClass()== UpcaseFilter.class);
    }

    //获取过滤器
    private DocumentFilter getFilter(CoursesPanel panel,String fieldName){
        JTextField field = panel.getField(fieldName);
        AbstractDocument document =(AbstractDocument) field.getDocument();
        return document.getDocumentFilter();
    }

    //检测文本框输入 此测试未通过,不排除是模拟器的问题
//    public void testKeyListeners() throws Exception {
//        sis.show();
//        JButton button = panel.getButton(CoursesPanel.ADD_BUTTON_NAME);
//        assertFalse(button.isEnabled());
//
//        selectField(CoursesPanel.DEPARTMENT_FIELD_NAME);
//        type('a');
//        selectField(CoursesPanel.NUMBER_FIELD_NAME);
//        type('1');
//
//
//        assertTrue(button.isEnabled());
//
//        panel.setText(CoursesPanel.DEPARTMENT_FIELD_NAME, " ");
//        sis.setAddButtonState();
//        assertFalse(button.isEnabled());
//
//        panel.setText(CoursesPanel.DEPARTMENT_FIELD_NAME, "a");
//        panel.setText(CoursesPanel.NUMBER_FIELD_NAME, " ");
//        sis.setAddButtonState();
//        assertFalse(button.isEnabled());
//
//    }

    //模拟输入
    private void type(int key) throws Exception {
        robot.keyPress(key);
        robot.keyRelease(key);
    }

    //模拟点击输入框获取光标
    private void selectField(String name) throws Exception {
        JTextField field = panel.getField(name);
        Point point = field.getLocationOnScreen();
        robot.mouseMove(point.x, point.y);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
    }


    //测试窗体能否显示
    public void testShow() {
        sis.show();
        assertTrue(frame.isVisible());
    }

    //关闭窗体
    protected void tearDown() {
        sis.close();
    }
}
